package com.yeck.springboot2;

import entity.InfoBack1;
import entity.InfoBack2;
import entity.InfoBack3;
import entity.InfoBack4;
import net.minidev.json.JSONObject;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


@RestController
@CrossOrigin
public class InfoController {
    String resource = "conf.xml";
    InputStream is = Resources.getResourceAsStream(resource);
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
    SqlSession session = sessionFactory.openSession();

    public InfoController() throws IOException {
    }

    @GetMapping("/getStudents")
    public List<InfoBack1> getStudents() {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.getStudents";
        return session.selectList(statement, null);
    }

    @PostMapping("/addStudent")
    public InfoBack2 addStudents(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.addStudents";
        int res = session.insert(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/editStudent")
    public InfoBack2 editStudents(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.editStudent";
        int res = session.update(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/delStudent")
    public InfoBack2 delStudent(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.delStudent";
        int res = session.delete(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/filterStudent")
    public List<InfoBack1> filterStudent(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.filterStudent";
        jsonParam.replace("name", "%" + jsonParam.get("name") + "%");
        jsonParam.replace("type", "%" + jsonParam.get("type") + "%");
        jsonParam.replace("Sclass", "%" + jsonParam.get("Sclass") + "%");
        if (jsonParam.get("age").equals("")){
            jsonParam.replace("age", "%");
        }
        if (jsonParam.get("sex").equals("")){
            jsonParam.replace("sex", "%");
        }
        if (jsonParam.get("idNo").equals("")){
            jsonParam.replace("idNo", "%");
        }
        return session.selectList(statement, jsonParam);
    }


    @GetMapping("/getStatus")
    public List<InfoBack3> getStatus(){
        String statement = "com.yeck.springboot2.Mapper.infoMapper.getStatus";
        return session.selectList(statement, null);
    }

    @PostMapping("/addStatus")
    public InfoBack2 addStatus(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.addStatus";
        int res = session.insert(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/editStatus")
    public InfoBack2 editStatus(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.editStatus";
        int res = session.update(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/delStatus")
    public InfoBack2 delStatus(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.delStatus";
        int res = session.delete(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/filterStatus")
    public List<InfoBack3> filterStatus(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.filterStatus";
        jsonParam.replace("name", "%" + jsonParam.get("name") + "%");
        jsonParam.replace("type", "%" + jsonParam.get("type") + "%");
        jsonParam.replace("Sclass", "%" + jsonParam.get("Sclass") + "%");
        jsonParam.replace("Ptype", "%" + jsonParam.get("Ptype") + "%");
        jsonParam.replace("time", "%" + jsonParam.get("time") + "%");
        jsonParam.replace("project", "%" + jsonParam.get("project") + "%");
        return session.selectList(statement, jsonParam);
    }

    @GetMapping("/getAbsence")
    public List<InfoBack4> getAbsence(){
        String statement = "com.yeck.springboot2.Mapper.infoMapper.getAbsence";
        return session.selectList(statement, null);
    }

    @PostMapping("/addAbsence")
    public InfoBack2 addAbsence(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.addAbsence";
        int res = session.insert(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/editAbsence")
    public InfoBack2 editAbsence(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.editAbsence";
        int res = session.update(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/delAbsence")
    public InfoBack2 delAbsence(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.delAbsence";
        int res = session.delete(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/filterAbsence")
    public List<InfoBack4> filterAbsence(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.filterAbsence";
        jsonParam.replace("name", "%" + jsonParam.get("name") + "%");
        jsonParam.replace("type", "%" + jsonParam.get("type") + "%");
        jsonParam.replace("Sclass", "%" + jsonParam.get("Sclass") + "%");
        jsonParam.replace("idNo", "%" + jsonParam.get("idNo") + "%");
        jsonParam.replace("time", "%" + jsonParam.get("time") + "%");
        return session.selectList(statement, jsonParam);
    }

    @GetMapping("/getClass")
    public List<entity.Class> getClasses(){
        String statement = "com.yeck.springboot2.Mapper.infoMapper.getClass";
        return session.selectList(statement, null);
    }

    @PostMapping("/addClass")
    public InfoBack2 addClass(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.addClass";
        int res = session.insert(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/editClass")
    public InfoBack2 editClass(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.editClass";
        int res = session.update(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/delClass")
    public InfoBack2 delClass(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.delClass";
        int res = session.delete(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @GetMapping("/getExam")
    public List<entity.Exam> getExam(){
        String statement = "com.yeck.springboot2.Mapper.infoMapper.getExam";
        return session.selectList(statement, null);
    }

    @PostMapping("/addExam")
    public InfoBack2 addExam(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.addExam";
        int res = session.insert(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/editExam")
    public InfoBack2 editExam(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.editExam";
        int res = session.update(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/delExam")
    public InfoBack2 delExam(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.delExam";
        int res = session.delete(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @GetMapping("/getScore")
    public List<entity.Score> getScore(){
        String statement = "com.yeck.springboot2.Mapper.infoMapper.getScore";
        return session.selectList(statement, null);
    }

    @PostMapping("/addScore")
    public InfoBack2 addScore(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.addScore";
        int res = session.insert(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/delScore")
    public InfoBack2 delScore(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.delScore";
        int res = session.delete(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @GetMapping("/getPaScore")
    public List<entity.Score> getPaScore(){
        String statement = "com.yeck.springboot2.Mapper.infoMapper.getBScore";
        return session.selectList(statement, null);
    }

    @PostMapping("/addPaScore")
    public InfoBack2 addPaScore(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.addBScore";
        int res = session.insert(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/delPaScore")
    public InfoBack2 delPaScore(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.delBScore";
        int res = session.delete(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @GetMapping("/getDept")
    public List<entity.Dept> getDept(){
        String statement = "com.yeck.springboot2.Mapper.infoMapper.getDept";
        return session.selectList(statement, null);
    }

    @PostMapping("/addDept")
    public InfoBack2 addDept(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.addDept";
        int res = session.insert(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/delDept")
    public InfoBack2 delDept(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.delDept";
        int res = session.delete(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }

    @PostMapping("/editDept")
    public InfoBack2 editDept(@RequestBody JSONObject jsonParam) {
        String statement = "com.yeck.springboot2.Mapper.infoMapper.editDept";
        int res = session.update(statement, jsonParam);
        session.commit();
        InfoBack2 infoBack2 = new InfoBack2();
        if (res == 1) {
            infoBack2.success = "success";
        } else {
            infoBack2.success = "false";
        }
        return infoBack2;
    }
}
